import sys

file = open("matrix.txt", "r")
array = [[int(x) for x in line.split(",")] for line in file]

#array = [[131, 673, 234, 103, 18],
#         [201, 96, 342, 965, 150],
#         [630, 803, 746, 422, 111],
#         [537, 699, 497, 121, 956],
#         [805, 732, 524, 37, 331]]
rows = len(array)
cols = len(array[0])

def neighbours(row, col):
    if row > 0: yield (row-1, col)
    if col > 0: yield (row, col-1)
    if row < rows - 1: yield (row+1, col)
    if col < cols - 1: yield (row, col+1)

sums = [[None for c in xrange(cols)] for r in xrange(rows)]
reachable = set()

reachable.add((0, 0))
sums[0][0] = array[0][0]

def minreachable():
    min = sys.maxint
    for (row, col) in reachable:
        if sums[row][col] < min:
            min = sums[row][col]
            (minrow, mincol) = (row, col)

    reachable.remove((minrow, mincol))
    return (minrow, mincol)

while None == sums[rows-1][cols-1]:
    (row, col) = minreachable()
    sum = sums[row][col]
    
    for (x,y) in neighbours(row, col):
        newsum = sum + array[x][y]
        if None == sums[x][y] or newsum < sums[x][y]:
            sums[x][y] = newsum
            reachable.add((x, y))

print sums[rows-1][cols-1]